print("Player stats Loading...");

local player_meta = FindMetaTable("Player") --Get the meta table of player

function player_meta:SetStats(level, xp, statpoints, money, weaponspeed, weapondamage, speed, clipsize, maxhealth, damagetaken, ownedbuyables, jump, spread)
	self:SetNetworkedInt( "inv_level", level )
	self:SetNetworkedInt( "inv_xp", xp )
	self:SetNetworkedInt( "inv_statpoints", statpoints )
	self:SetNetworkedInt( "inv_money", money )
	self:SetNetworkedInt( "inv_weaponspeed", weaponspeed )
	self:SetNetworkedInt( "inv_weapondamage", weapondamage )
	self:SetNetworkedInt( "inv_speed", speed )
	self:SetNetworkedInt( "inv_clipsize", clipsize )
	self:SetNetworkedInt( "inv_maxhealth", maxhealth )
	self:SetNetworkedInt( "inv_damagetaken", damagetaken )
	self:SetNetworkedInt( "inv_jump", jump )
	self:SetNetworkedInt( "inv_spread", spread )
	self:SetNetworkedString( "inv_ownedbuyables", glon.encode(ownedbuyables) )
	
	self:SaveStats()
end
 
function player_meta:SaveStats()
	local level = self:GetLevel()
	local xp = self:GetXP()
	local points = self:GetStatpoints()
	local money = self:GetMoney()
	local weaponspeed = self:GetWeaponSpeed()
	local weapondamage = self:GetWeaponDamage()
	local speed = self:GetSpeed()
	local clipsize = self:GetClipSize()
	local maxhealth = self:GetMaxHealth()
	local damagetaken = self:GetDamageTaken()
	local ownedbuyables = self:GetOwnedBuyables()
	local jump  = self:GetJump()
	local spread  = self:GetSpread()
	
	self:SetPData("inv_level", level)
	self:SetPData("inv_xp", xp)
	self:SetPData("inv_statpoints", points)
	self:SetPData("inv_money", money)
	self:SetPData( "inv_weaponspeed", weaponspeed )
	self:SetPData( "inv_weapondamage", weapondamage )
	self:SetPData( "inv_speed", speed )
	self:SetPData( "inv_clipsize", clipsize )
	self:SetPData( "inv_maxhealth", maxhealth )
	self:SetPData( "inv_damagetaken", damagetaken )
	self:SetPData( "inv_ownedbuyables", ownedbuyables )
	self:SetPData( "inv_jump", jump )
	self:SetPData( "inv_spread", spread )
end

function player_meta:DeductMoney(amount)
	local currentmoney = self:GetMoney()
	self:SetNetworkedInt( "inv_money", currentmoney - amount )
	self:SaveStats()
end

function player_meta:AddMoney(amount)
	local currentmoney = self:GetMoney()
	self:SetNetworkedInt( "inv_money", currentmoney + amount )
	self:SaveStats()
end

function player_meta:DeductStatPoints(amount)
	local currentpoints = self:GetStatpoints()
	self:SetNetworkedInt( "inv_statpoints", currentpoints - amount )
	self:SaveStats()
end

function player_meta:AddXP(amount)
	local currentxp = self:GetXP()
	self:SetNetworkedInt( "inv_xp", currentxp + amount )
	self:SaveStats()
end

function player_meta:SetXP(amount)
	self:SetNetworkedInt( "inv_xp",amount )
	self:SaveStats()
end

function player_meta:SetLevel(amount)
	self:SetNetworkedInt( "inv_level",amount )
	self:SaveStats()
end

function player_meta:AddOwnedBuyables(ID)
	local currentownedbuyables = glon.decode(self:GetOwnedBuyables())
	
	-- Is this an upgrade from an old level, if so lets remove the old level...
	if(INV_BUYABLES[ID][7] != nil)then
		-- go through previous levels
		for k, v in pairs(INV_BUYABLES[ID][7]) do
			-- do we have this old level in our pocket?
			if(table.HasValue(currentownedbuyables, v))then
				-- Remove it, find where it is...
				for i=1, #currentownedbuyables do
					if(currentownedbuyables[i] == v)then
						--table.remove(currentownedbuyables, i)
					end
				end
			end
		end
	end
	
	table.insert(currentownedbuyables, ID)
	self:SetNetworkedString( "inv_ownedbuyables", glon.encode(currentownedbuyables) )
	self:SaveStats()
end

function player_meta:AddStatPoints(amount)
	local currentstatpoints = self:GetStatpoints()
	currentstatpoints = currentstatpoints + amount
	self:SetNetworkedString( "inv_statpoints", currentstatpoints )
	self:SaveStats()
end

function player_meta:AddJump(amount)
	local currentjump = self:GetJump()
	currentjump = currentjump + amount
	self:SetNetworkedString( "inv_jump", currentjump )
	self:SaveStats()
end

function player_meta:AddSpeed(amount)
	local currentspeed = self:GetSpeed()
	currentspeed = currentspeed + amount
	self:SetNetworkedString( "inv_speed", currentspeed )
	self:SaveStats()
end

function player_meta:AddMaxHealth(amount)
	local currenthealth = self:GetMaxHealth()
	currenthealth = currenthealth + amount
	self:SetNetworkedString( "inv_maxhealth", currenthealth )
	self:SaveStats()
end

function player_meta:AddDamageTaken(amount)
	local currentdamtaken = self:GetDamageTaken()
	currentdamtaken = currentdamtaken + amount
	self:SetNetworkedString( "inv_damagetaken", currentdamtaken )
	self:SaveStats()
end

function player_meta:AddSpread(amount)
	local currentspread = self:GetSpread()
	currentspread = currentspread + amount
	self:SetNetworkedString( "inv_spread", currentspread )
	self:SaveStats()
end

function player_meta:AddClipSize(amount)
	local currentclipsize = self:GetClipSize()
	currentclipsize = currentclipsize + amount
	self:SetNetworkedString( "inv_clipsize", currentclipsize )
	self:SaveStats()
end

function player_meta:AddWeaponDamage(amount)
	local currentweapondamage = self:GetWeaponDamage()
	currentweapondamage = currentweapondamage + amount
	self:SetNetworkedString( "inv_weapondamage", currentweapondamage )
	self:SaveStats()
end

function player_meta:AddWeaponSpeed(amount)
	local currentweaponspeed = self:GetWeaponSpeed()
	currentweaponspeed = currentweaponspeed + amount
	self:SetNetworkedString( "inv_weaponspeed", currentweaponspeed )
	self:SaveStats()
end


-------------
-- GETTERS --
-------------
function player_meta:GetWeaponSpeed()
	return self:GetNetworkedInt( "inv_weaponspeed" )
end
function player_meta:GetWeaponDamage()
	return self:GetNetworkedInt( "inv_weapondamage" )
end
function player_meta:GetSpeed()
	return self:GetNetworkedInt( "inv_speed" )
end
function player_meta:GetClipSize()
	return self:GetNetworkedInt( "inv_clipsize" )
end
function player_meta:GetMaxHealth()
	return self:GetNetworkedInt( "inv_maxhealth" )
end
function player_meta:GetDamageTaken()
	return self:GetNetworkedInt( "inv_damagetaken" )
end
function player_meta:GetLevel()
	return self:GetNetworkedInt( "inv_level" )
end
function player_meta:GetXP()
	return self:GetNetworkedInt( "inv_xp" )
end
function player_meta:GetStatpoints()
	return self:GetNetworkedInt( "inv_statpoints" )
end
function player_meta:GetMoney()
	return self:GetNetworkedInt( "inv_money" )
end
function player_meta:GetJump()
	return self:GetNetworkedInt( "inv_jump" )
end
function player_meta:GetSpread()
	return self:GetNetworkedInt( "inv_spread" )
end

function player_meta:GetOwnedBuyables()
	return self:GetNetworkedString( "inv_ownedbuyables" )
end